import com.machinsight.library.dao.bean.UserInfoEntity;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.io.Reader;

/**
 * Copyright ©2017-2020 Machinsight Technology Co., Ltd.All rights reserved.
 * Author: Zhoushaohua
 * Date: 2020/1/2
 * Description:测试数据库单元测试类
 */
public class MyBatisJunit {
    private static SqlSessionFactory sqlSessionFactory;

    @BeforeClass
    public static void init() {
        try {
            Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void selectUserById() {
        if (sqlSessionFactory == null) {
            System.out.println("sqlSessionFactory is null!");
            return;
        }
        SqlSession sqlSession = null;
        try {
            sqlSession = sqlSessionFactory.openSession();
            UserInfoEntity userInfoEntity = sqlSession.selectOne("com.machinsight.library.dao.mapper.IUserMapper.findUserById", 1);
            System.out.println(userInfoEntity);
        } catch (Exception e) {
            System.out.println(e.toString());
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    @Test
    public void addUser() {
        if (sqlSessionFactory == null) {
            System.out.println("sqlSessionFactory is null!");
            return;
        }
        SqlSession sqlSession = null;
        try {
            sqlSession = sqlSessionFactory.openSession();
            UserInfoEntity entity = new UserInfoEntity();
            entity.setUserId(10011);
            entity.setName("消防支队-虹口大队");
            entity.setPassword("123458794541cafvf");
            entity.setPhone("119");
            entity.setEmail("119@xf.com");
            entity.setSex("men");
            int result = sqlSession.insert("com.machinsight.library.dao.mapper.IUserMapper.addUser", entity);
            System.out.println(result);
            sqlSession.commit();//修改数据库，需要提交事务，不然不会写入数据库
            System.out.println(entity.toString());
        } catch (Exception e) {
            System.out.println(e.toString());
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    @Test
    public void updateUser() {
        if (sqlSessionFactory == null) {
            System.out.println("sqlSessionFactory is null!");
            return;
        }
        SqlSession sqlSession = null;
        try {
            sqlSession = sqlSessionFactory.openSession();
            UserInfoEntity entity = new UserInfoEntity();
            entity.setId(3);
            entity.setUserId(10011);
            entity.setName("消防支队-虹口分队");
            entity.setPassword("qwerrtt");
            entity.setPhone("119");
            entity.setEmail("119@xf.com");
            entity.setSex("men");
            int result = sqlSession.update("com.machinsight.library.dao.mapper.IUserMapper.updateUser", entity);
            System.out.println(result);
            sqlSession.commit();//修改数据库，需要提交事务，不然不会写入数据库
            System.out.println(entity.getId());
        } catch (Exception e) {
            System.out.println(e.toString());
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    @Test
    public void deleteUserByName() {
        if (sqlSessionFactory == null) {
            System.out.println("sqlSessionFactory is null!");
            return;
        }
        SqlSession sqlSession = null;
        try {
            sqlSession = sqlSessionFactory.openSession();
            int result = sqlSession.delete("com.machinsight.library.dao.mapper.IUserMapper.deleteUserByName", "消防支队-虹口大队");
            System.out.println(result);
            sqlSession.commit();//修改数据库，需要提交事务，不然不会写入数据库
        } catch (Exception e) {
            System.out.println(e.toString());
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
}
